<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="title">
        <img src="@/assets/images/logo.png" alt />
        <span>Ryzen Soft Manage</span>
      </div>
      <el-button type="info" @click="logout">Quit</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 左侧侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu
          background-color="#323642"
          text-color="#fff"
          active-text-color="#4098fe"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePathName"
        >
          <!-- 设置只能展开一个菜单 -->
          <div class="toggleMenuButton" @click="toggleMenu">|||</div>
          <!-- 一级菜单 -->
          <el-submenu :index="'/'+item.path" v-for="item in menulist" :key="item.id">
            <!-- 菜单模版区域 -->
            <template slot="title">
              <i :class="iconsOBJ[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="activeName('/'+subItem.path)"
            >
              <!-- 菜单模版区域 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主题 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      // 定义一级菜单的每一个图标
      iconsOBJ: {
        '125': 'el-icon-user-solid',
        '103': 'el-icon-s-check',
        '101': 'el-icon-s-goods',
        '102': 'el-icon-s-order',
        '145': 'el-icon-s-data'
      },
      isCollapse: false,
      activePathName: ''
    }
  },
  created () {
    this.getMenuList()
    // 为了做到页面切换的时候能根据地址栏的路径，来激活这个菜单当前高亮的选项
    this.activePathName = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push({ name: 'login' })
    },
    // 获取侧边栏数据
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(res)
    },
    toggleMenu () {
      this.isCollapse = !this.isCollapse
    },
    activeName (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePathName = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #343a3d;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .title {
    height: 60px;
    display: flex;
    align-items: center;
    img {
      height: 80%;
      padding-right: 10px;
    }
    span {
      color: #fff;
      font-size: 20px;
    }
  }
}

.el-aside {
  background-color: #323642;
}

.el-main {
  background-color: #d9dde0;
}

.home-container {
  height: 100%;
}

.el-menu {
  border-right: 0;
  .toggleMenuButton {
    line-height: 24px;
    font-size: 10px;
    color: #fff;
    background-color: #636c7c;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
</style>
